$(function() {
	
	//链接
	$(document).on("click", ".tohuowei", function() {
		//仓库id
		var warehouseId = GetQueryString("warehouseId");
		console.log("warehouseId    " + warehouseId)
		//货区name
		var h = $(this).parent().parent().parent().parent().parent().children().children().text();
		var huoquName = h.slice(0, 2);
		//货层
		var huoceng = $(this).text();
		window.location.href = "Goodsallocation.html?warehouseId=" + warehouseId + "&huoquName=" + huoquName + "&huoceng=" + huoceng;
	})
	
	//弹出层链接   货区有问题
	$(document).on("click", ".tohuowei2", function() {
		//仓库id
		var warehouseId = GetQueryString("warehouseId");
		console.log("warehouseId    " + warehouseId)
		//货区name
		var huoquName = $(this).parent().parent().parent().prev().children().children().text();
		//货层
		var huoceng = $(this).text();
		window.location.href = "Goodsallocation.html?warehouseId=" + warehouseId + "&huoquName=" + huoquName + "&huoceng=" + huoceng;
	})

	/* layui的弹出层 */
	layui.use(['jquery', 'layer'], function() {
		var $ = layui.jquery;
		var layer = layui.layer;
	
		$(document).on("click", ".cli", function() {
			//货区name
			var h = $(this).parent().parent().parent().parent().parent().children().children().text();
			var huoquName = h.slice(0, 2);
			var warehouseId = GetQueryString("warehouseId");
			//测试拼接div块
			loadTanchuDiv();
			
			function loadTanchuDiv() {
				$.ajax({
					url: "http://hjp.free.idcfengye.com/ajaxdivTanchu?kuhao="+warehouseId+"&huoqu="+huoquName,
					type: 'GET',
					dataType: 'json',
					success: function(data) {
						//小标题
						var sb='<span class="goodsshelves-num"><span class="hjnum">'/* +warehouseId */+huoquName+'</span></span>';
						$(".goodsshelves-title").append(sb);
						/* console.log(data);
						console.log(data.length); */
						 var num = data.length;
						var hang = num / 8; 
						//num此处为各货区的货架数
						/* var num = Math.ceil(Math.random() * 40);
						console.log("num         "+num)
						var hang = num / 8; */
						for (var i = 0; i < hang; i++) {
							var begin = 0 + 8 * i;
							var end = 8 + i * 8;
							var sb='<div class="three">';
							sb+='</div>';
							$(".conentbox2").append(sb);
							var hjarr = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20","21", "22", "23", "24", "25", "26", "27", "28", "29", "30","31", "32", "33", "34", "35", "36", "37", "38", "39", "40");
							console.log("begin   " + begin);
							console.log("end   " + end);
							if(end < num){
								for (var j = begin; j < end; j++) {
									var sb='<div class="three-box">				';
									sb+='	<div class="three-box-font tohuowei2">'+hjarr[j]+'</div>';
									sb+='</div>';
									$(".three").last().append(sb);
								}
							}else if(end > num){
								var yu = num % 8;
								console.log("弹出层余数："+yu)
								for (var k1 = begin; k1 < (begin+yu); k1++) {
									var sb='<div class="three-box">				';
									sb+='	<div class="three-box-font tohuowei2">'+hjarr[k1]+'</div>';
									sb+='</div>';
									$(".three").last().append(sb);
								}
							}
						}
					},
				});	
			};
			openAddUser();
		})
		
		var url;
		var mainIndex;
		//打开添加页面
		function openAddUser() {
			mainIndex = layer.open({
				type: 1,
				title: '货区管理',
				content: $("#saveOrUpdateDiv"),
				area: ['70%', '90%'],
				success: function(index) {

				},
				end: function () {
					//重载，解决重复累加问题
				    location.reload();
				}
			});
		}	
	});

	

	//截取地址栏参数
	function GetQueryString(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return decodeURI(r[2]);
		return null;
	}

	//拼接div块
	loadDivSet();

	function loadDivSet() {
		$.ajax({
			url: "http://hjp.free.idcfengye.com/ajaxdiv",
			type: 'GET',
			dataType: 'json',
			success: function(data) {
				console.log(data);
				console.log(data.length);
				var sb = "";
				for (var i = 0; i < data.length; i++) {
					//感觉就是拿到区的所有排，然后用,分割开，然后放入数组，然后就可以通过for循环遍历下标拿到
					var sb = '<div class="div-set">';
					sb += '					<div class="div-set-head">';
					sb += '						<div class="div-set-head-text">' + data[i].area_name + '</div>';
					sb += '					</div>';
					sb += '					<div class="right">';
					sb += '					</div>';
					sb += '				</div>	';
					sb += '				';
					$(".conentbox").append(sb);

					//num此处为各货区的货架数
					var num = Math.ceil(Math.random() * 36);
					console.log(num);

					//注意：num不能超过5个
					var hang = num / 3;
					if (hang > 5) {
						hang = 5;
					}

					for (var j = 0; j < hang; j++) {
						var begin = 0 + 3 * j;
						var end = 3 + j * 3;

						var sb =
							'<div class="div-group" style="margin-top: 20px; margin-bottom: 10px; height: 15%; margin-right: auto;">';
						sb += '						</div>';
						$(".right").last().append(sb);

						var hjarr = new Array("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16");

						console.log("begin   " + begin);
						console.log("end   " + end);

						if (hjarr.length % 3 != 0 && hjarr.length < 15) {
							console.log("xxxxx " + hjarr.length % 3);
							if (hjarr.length == 1) {
								var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
								sp += '								<div class="text">';
								sp += '									<div class="aaa more tohuowei">' + hjarr[0] + '</a></div>';
								sp += '								</div>							';
								sp += '							</div>';
								$(".div-group").last().append(sp);
								for (var k = 0; k < 2; k++) {
									var sp = '							<div class="right-box" style=" height: 10%; width: 30%; display: inline-block;">';
									sp += '								<div class="text2">';
									sp += '									<div class="more2"></div>';
									sp += '								</div>							';
									sp += '							</div>';
									$(".div-group").last().append(sp);
								}
							} else if (hjarr.length == 2) {
								for (var k = 0; k < 2; k++) {
									var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
									sp += '								<div class="text">';
									sp += '									<div class="aaa more tohuowei">' + hjarr[k] + '</a></div>';
									sp += '								</div>							';
									sp += '							</div>';
									sp += '							';
									$(".div-group").last().append(sp);
								}
								for (var k = 0; k < 1; k++) {
									var sp = '							<div class="right-box" style=" height: 10%; width: 30%; display: inline-block;">';
									sp += '								<div class="text2">';
									sp += '									<div class="more2"></div>';
									sp += '								</div>							';
									sp += '							</div>';
									$(".div-group").last().append(sp);
								}
							} else if (hjarr.length > 3) {
								if (end < hjarr.length) {
									for (var k = begin; k < end; k++) {
										var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
										sp += '								<div class="text">';
										sp += '									<div class="aaa more tohuowei">' + hjarr[k] + '</a></div>';
										sp += '								</div>							';
										sp += '							</div>';
										sp += '							';
										$(".div-group").last().append(sp);
									}
								} else if (end > hjarr.length) {
									if (hjarr.length % 3 == 1) {
										var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
										sp += '								<div class="text">';
										sp += '									<div class="aaa more tohuowei">' + hjarr[begin] + '</a></div>';
										sp += '								</div>							';
										sp += '							</div>';
										$(".div-group").last().append(sp);
										for (var k = 0; k < 2; k++) {
											var sp = '							<div class="right-box" style=" height: 10%; width: 30%; display: inline-block;">';
											sp += '								<div class="text2">';
											sp += '									<div class="more2"></div>';
											sp += '								</div>							';
											sp += '							</div>';
											$(".div-group").last().append(sp);
										}
									} else if (hjarr.length % 3 == 2) {
										for (var k = begin; k < end - 1; k++) {
											var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
											sp += '								<div class="text">';
											sp += '									<div class="aaa more tohuowei">' + hjarr[k] + '</a></div>';
											sp += '								</div>							';
											sp += '							</div>';
											sp += '							';
											$(".div-group").last().append(sp);
										}
										for (var k = 0; k < 1; k++) {
											var sp = '							<div class="right-box" style=" height: 10%; width: 30%; display: inline-block;">';
											sp += '								<div class="text2">';
											sp += '									<div class="more2"></div>';
											sp += '								</div>							';
											sp += '							</div>';
											$(".div-group").last().append(sp);
										}
									}
								}
							}
						} else if (hjarr.length % 3 == 0 && hjarr.length <= 15) {
							for (var k = begin; k < end; k++) {
								var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
								sp += '								<div class="text">';
								sp += '									<div class="aaa more tohuowei">' + hjarr[k] + '</a></div>';
								sp += '								</div>							';
								sp += '							</div>';
								sp += '							';
								$(".div-group").last().append(sp);
							}
						} else if (hjarr.length > 15) {
							console.log("多出的end  " + end);
							if (end <= 12) {
								for (var k = begin; k < end; k++) {
									var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
									sp += '								<div class="text">';
									sp += '									<div class="aaa more tohuowei">' + hjarr[k] + '</a></div>';
									sp += '								</div>							';
									sp += '							</div>';
									sp += '							';
									$(".div-group").last().append(sp);
								}
							} else {
								for (var k = 12; k < 14; k++) {
									var sp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
									sp += '								<div class="text">';
									sp += '									<div class="aaa more tohuowei">' + hjarr[k] + '</a></div>';
									sp += '								</div>							';
									sp += '							</div>';
									sp += '							';
									$(".div-group").last().append(sp);
								}
								var spp = '							<div class="right-box" style="height: 10%; width: 30%; display: inline-block;">';
								spp += '								<div class="text">';
								spp += '									<div class="aaa more cli" id="onclick">' + "更多" + '</a></div>';
								spp += '								</div>							';
								spp += '							</div>';
								spp += '							';
								$(".div-group").last().append(spp);
							}
						}
					}
				}
			},
			error: function() {
				alert("error");
			}
		});
	};

});
	
